package com.yuyou.fn.crm.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.Threads;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.crm.entity.CrmDepartment;
import com.yuyou.fn.crm.mapper.CrmDepartmentMapper;
import com.yuyou.fn.crm.service.ICrmDepartmentService;
import com.yuyou.fn.crm.service.ICustomerResourceService;
import com.yuyou.fn.crm.vo.CrmDepartmentVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * CrmDepartment 表数据服务层接口实现类
 *
 */
@Service
public class CrmDepartmentServiceImpl extends SuperServiceImpl<CrmDepartmentMapper, CrmDepartment,Long> implements ICrmDepartmentService {

    @Resource
    private ICustomerResourceService customerResourceService;

    @Override
    @Transactional
    public void saveOrUpdateCrmDepartment(Long areaTeamId, List<CrmDepartmentVo> crmDepartmentVoList) {
        if (areaTeamId==null || Utils.checkListEmpty(crmDepartmentVoList)){
            throw new BusException("区域或者部门信息不可为空");
        }
        Principals.Principal principal=Principals.get();
        if (principal==null){
            throw new BusException("缺乏操作人");
        }
        this.deleteByAreaTeamId(areaTeamId);
        List<CrmDepartment> saveList=new ArrayList<>(crmDepartmentVoList.size());
        final List<CrmDepartment> recordChange=new ArrayList<>(crmDepartmentVoList.size());
        for (CrmDepartmentVo crmDepartmentVo:crmDepartmentVoList){
            if (StringUtils.isEmpty(crmDepartmentVo.getDepartmentName())){
                throw new BusException("存在部门名称为空");
            }
            CrmDepartment crmDepartment = new CrmDepartment();
            crmDepartment.setDepartmentId(crmDepartmentVo.getDepartmentId());
            crmDepartment.setAreaTeamId(areaTeamId);
            crmDepartment.setDepartmentName(crmDepartmentVo.getDepartmentName());
            crmDepartment.setOperatorId(principal.id);
            crmDepartment.setOperatorName(principal.name);
            if (crmDepartment.getDepartmentId()==null){
                crmDepartment.setCreateTime(TimeUtils.now());

            }else {
                recordChange.add(crmDepartment);
            }
            crmDepartment.setUpdateTime(TimeUtils.now());
            saveList.add(crmDepartment);
        }
        if (recordChange.size()>0){
            Threads.dispatch(new Runnable() {
                @Override
                public void run() {
                    for (CrmDepartment crmDepartment:recordChange){
                        customerResourceService.updateDepartmentNameById(crmDepartment.getDepartmentId(),crmDepartment.getDepartmentName());
                    }
                }
            });
        }

        this.insertBatch(saveList);
    }

    @Override
    public void deleteByAreaTeamId(Long areaTeamId) {
        if (areaTeamId==null){
            throw new NullPointerException();
        }
        Example<CrmDepartment> example=CrmDepartment.createExample();
        example.createCriteria().andEqualTo("areaTeamId",areaTeamId);
        this.deleteByExample(example);
    }

    @Override
    public List<CrmDepartment> findDepartmentListByAreaTeamId(Long areaTeamId) {
        if (areaTeamId==null){
            throw new NullPointerException();
        }
        Example<CrmDepartment> example=CrmDepartment.createExample();
        example.createCriteria().andEqualTo("areaTeamId",areaTeamId);
        return findListByExample(example);
    }

    @Override
    public CrmDepartment findByAreaTeamIdAndName(long areaTeamId, String departmentName) {
        Example<CrmDepartment> example=CrmDepartment.createExample();
        example.createCriteria().andEqualTo("areaTeamId",areaTeamId).andEqualTo("departmentName",departmentName);
        return findOneByExample(example);
    }
}